<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:doc="livcos.org/ns/doc"
  xmlns:f="livcos.org/ns/xslt/function"
  xmlns="http://www.w3.org/1999/xhtml"
  exclude-result-prefixes="doc f">

	<xsl:import href="embedding.xsl" />
	
	<xsl:template match="doc:link">
		<xsl:variable name="invalidRef" select="not(f:isValidUrl(@ref))"/>
		<a id="{@id}">
			<xsl:attribute name="href">
				<xsl:call-template name="link2Url">
					<xsl:with-param name="link" select="."/>
				</xsl:call-template>
			</xsl:attribute>
			<xsl:if test="(@ref = '#') and (normalize-space(@url) = '')">
				<xsl:attribute name="class">default</xsl:attribute>
			</xsl:if>
			<xsl:apply-templates select="@note"/>
			<xsl:apply-templates select="node()"/>
			<xsl:if test="normalize-space(.) = ''">
				<xsl:choose>
					<xsl:when test="starts-with(@ref, 'javascript:')">
						<xsl:value-of select="@ref"/>
					</xsl:when>
					<xsl:when test="$invalidRef">
						<xsl:value-of select="@ref"/>
					</xsl:when>
					<xsl:when test="not(contains(@ref, '://')) and (normalize-space(@ref) != '') and (@ref != '#')">
						<xsl:apply-templates select="@ref" mode="setRefEntity"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="@ref"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:if>
			<xsl:if test="$invalidRef and not(starts-with(@ref, 'javascript:'))">
				<span style="color: red;"> [Invalid URL!]</span>
			</xsl:if>
		</a>
	</xsl:template>
	
	<xsl:template name="link2Url">
		<xsl:param name="link"/>
		<xsl:choose>
			<xsl:when test="normalize-space($link/@ref) != ''">
				<xsl:choose>
					<xsl:when test="normalize-space($link/@view) != ''">
						<xsl:call-template name="ref2Url">
							<xsl:with-param name="ref" select="$link/@view"/>
							<xsl:with-param name="default_extension" select="'.html'"/>
							<xsl:with-param name="default_view" select="'web'"/>
						</xsl:call-template>
						<xsl:text>?ref=</xsl:text>
						<xsl:value-of select="replace(f:resolve-object-id($link/@ref, $link), '#', '&amp;entity=')"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:call-template name="ref2Url">
							<xsl:with-param name="ref" select="$link/@ref"/>
							<xsl:with-param name="default_extension" select="'.html'"/>
							<xsl:with-param name="default_view" select="'web'"/>
						</xsl:call-template>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:when test="normalize-space(@url) != ''">
				<xsl:value-of select="@url"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:text>#</xsl:text>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="doc:link" mode="createWithRefEntity">
    <xsl:param name="refEntity"/>
    <xsl:choose>
      <xsl:when test="$refEntity/doc:title">
        <xsl:value-of select="$refEntity/doc:title"/>
      </xsl:when>
      <xsl:when test="$refEntity/doc:label">
        <xsl:value-of select="$refEntity/doc:label"/>
      </xsl:when>
      <xsl:when test="$refEntity/@name">
        <xsl:value-of select="$refEntity/@name"/>
      </xsl:when>
      <xsl:when test="$refEntity/doc:document/doc:head/doc:title">
        <xsl:value-of select="$refEntity/doc:document/doc:head/doc:title"/>
      </xsl:when>
      <xsl:when test="$refEntity/doc:document/doc:head/doc:name">
        <xsl:value-of select="$refEntity/doc:document/doc:head/doc:name"/>
      </xsl:when>
      <xsl:when test="$refEntity/doc:document/doc:head/doc:context">
        <xsl:value-of select="$refEntity/doc:document/doc:head/doc:context"/>
      </xsl:when>
      <xsl:when test="$refEntity/*[local-name() = 'title']">
        <xsl:value-of select="$refEntity/*[local-name() = 'title']"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="@ref"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template match="doc:link/@note">
    <xsl:attribute name="title"><xsl:value-of select="."/></xsl:attribute>
  </xsl:template>
  
</xsl:stylesheet>
